package domain.entities;// default package

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Regions entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .Regions
 * @author MyEclipse Persistence Tools
 */

public class RegionsDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(RegionsDAO.class);
	// property constants
	public static final String REGION = "region";
	public static final String CODE = "code";
	public static final String ADM1CODE = "adm1code";

	protected void initDao() {
		// do nothing
	}

	public void save(Regions transientInstance) {
		log.debug("saving Regions instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Regions persistentInstance) {
		log.debug("deleting Regions instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Regions findById(Short id) {
		log.debug("getting Regions instance with id: " + id);
		try {
			Regions instance = (Regions) getHibernateTemplate().get("Regions",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Regions> findByExample(Regions instance) {
		log.debug("finding Regions instance by example");
		try {
			List<Regions> results = (List<Regions>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

    public List<Regions> findByCountry(String propertyName, Object value) {
        log.debug("finding Regions instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Regions where "
                    + propertyName + "= " +value;
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Regions instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Regions as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

   public List<Regions> findByRegion(Object region) {
        return findByProperty(REGION, region);
    }

    public Regions findByRegionAndCountry(String region, String country) {
        log.debug("finding Regions instance with region and country");
        try {
            String queryString = "from Regions where region like '"
                    + region + "' and countryId = " + country;

            return (Regions) getHibernateTemplate().find(queryString).get(0);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Regions> findByCode(Object code) {
		return findByProperty(CODE, code);
	}

	public List<Regions> findByAdm1code(Object adm1code) {
		return findByProperty(ADM1CODE, adm1code);
	}

	public List findAll() {
		log.debug("finding all Regions instances");
		try {
			String queryString = "from Regions";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Regions merge(Regions detachedInstance) {
		log.debug("merging Regions instance");
		try {
			Regions result = (Regions) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Regions instance) {
		log.debug("attaching dirty Regions instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Regions instance) {
		log.debug("attaching clean Regions instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RegionsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RegionsDAO) ctx.getBean("RegionsDAO");
	}
}